package com.example.menhoo.toollibrary.common.helper;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by yanglei on 15/11/8.
 */

public class DateUtil {
    /**yyyy-MM-dd HH:mm:ss*/
    public static String TimeFormatOne="yyyy-MM-dd HH:mm:ss";
    /**yyyy-MM-dd HH:mm*/
    public static String TimeFormatOne2="yyyy-MM-dd HH:mm";
    /**yyyy-MM-dd HH:mm:ss:ff*/
    public static String TimeFormatOne3="yyyy-MM-dd HH:mm:ss:ff";
    /**yyyy/MM/dd HH:mm:ss*/
    public static String TimeFormatTwo="yyyy/MM/dd HH:mm:ss";
    /**yyyy年MM月dd日 HH:mm:ss*/
    public static String TimeFormatThree="yyyy年MM月dd日 HH:mm:ss";
    /**yy-MM-dd EEEE HH:mm*/
    public static String TimeFormatFour="yy-MM-dd EEEE HH:mm";

    /**
     * 得到当前时间
     * */
    public static Date Now()
    {
        return new Date(System.currentTimeMillis());
    }
    /**
     * 把这样的json时间格式"/Date(1368083400000)/" 转换为 Date
     * */
    public static Date JsonDateStringToDate(String JsonDateString)
    {
        String jsonDate = JsonDateString.replace("/Date(", "");
        jsonDate = jsonDate.replace(")/", "");
        if(jsonDate.indexOf("+")>=0){
            jsonDate = jsonDate.split("\\+")[0];
        }

        Date date_ =null;
        try {
            date_ = new Date(Long.valueOf(jsonDate));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return date_;

    }
    /**
     * 把这样的json时间格式"/Date(1368083400000)/" 转换为 时间字符串
     * */
    public static String JsonDateStringToStr(String JsonDateString,String format)
    {
        SimpleDateFormat formatDate = new SimpleDateFormat(format);
        Date date_ =JsonDateStringToDate(JsonDateString);
        String tiemStr = null;
        try {
            tiemStr = formatDate.format(date_);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return tiemStr;

    }
    /**
     * 将yyyy-MM-dd HH:mm:ss格式的字符串转换为date
     * */
    public static Date StringToDate(String time)
    {
        SimpleDateFormat formatDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date_ =null;
        try {
            date_ = formatDate.parse(time);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return date_;
    }
    /**
     * 将字符串转换为date
     * */
    public static Date StringToDate(String time,String format)
    {
        SimpleDateFormat formatDate = new SimpleDateFormat(format);
        Date date_ =null;
        try {
            date_ = formatDate.parse(time);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return date_;
    }
    /**
     * 将date转换为String
     * */
    public static String DateToString(Date time,String format)
    {
        SimpleDateFormat formatDate = new SimpleDateFormat(format);
        String re=null;
        try {
            re = formatDate.format(time);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return re;
    }
    /**
     *
     * @param milliseconds 毫秒数
     * @return 简单的默认格式日期时间
     */
    public static String getSimpleDatetime(long milliseconds) {
        Date date = new Date(milliseconds);
        DateFormat sdf = SimpleDateFormat.getDateTimeInstance();
        return sdf.format(date);
    }

    /**
     * 输出时间
     * @param milliseconds 毫秒数
     * @param format 输出格式
     * @return
     */
    public static String getDatetime(long milliseconds,String format) {
        Date date = new Date(milliseconds);
        DateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(date);
    }
    /**
     * "yyyy-MM-dd HH:mm:ss Z"
     * @param milliseconds 毫秒数
     * @return 默认上述格式的时间
     */
    public static String getDefaultDatetime(long milliseconds) {
        Date date = new Date(milliseconds);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z");
        return sdf.format(date);
    }

    /**
     * "yyyy-MM-dd HH:mm:ss"
     * @param milliseconds 毫秒数
     * @return 默认上述格式的时间
     */
    public static String getTimestampDatetime(long milliseconds) {
        Date date = new Date(milliseconds);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(date);
    }

    /**
     * "20100315072741000-0700" =>  "yyyy-MM-dd HH:mm:ss Z"
     * ex. 可用于Ocean返回的时间格式的转换
     * @param strTime
     * @return
     */
    public static String getTimeString(String strTime) {
        try {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmssSSSZ");
            Date d = formatter.parse(strTime);
            DateFormat df1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z");

            return df1.format(d);
        } catch (ParseException e) {
            return strTime;
        } catch (NullPointerException npe) {
            return "";
        }
    }

    /**
     * 得到一个短格式的时间字符串
     */
    public static String getShotTimeStr(Date date){
        Date now =Now();
        if(date!=null){
            if(now.getYear()-date.getYear()>0){
                return DateToString(date,"yyyy年");
            }
            if(now.getDate()-date.getDate()==0){
                //当天
                return DateToString(date,"HH:mm");
            }
            else{
                //以前
                return DateToString(date,"MM月dd日");
            }
        }
        else{
            return null;
        }
    }
    /**
     * 两个时间相差距离多少天多少小时多少分多少秒
     * @param str1 时间参数 1 格式：1990-01-01 12:00:00
     * @param str2 时间参数 2 格式：2009-01-01 12:00:00
     * @param format 格式
     * @return String 返回值为：xx天xx小时xx分xx秒
     */
    public static String getDistanceTime(String str1, String str2 ,String format) {
        DateFormat df = new SimpleDateFormat(format);
        Date one;
        Date two;
        long day = 0;
        long hour = 0;
        long min = 0;
        long sec = 0;
        try {
            one = df.parse(str1);
            two = df.parse(str2);
            long time1 = one.getTime();
            long time2 = two.getTime();
            long diff ;
            if(time1<time2) {
                diff = time2 - time1;
            } else {
                diff = time1 - time2;
            }
            day = diff / (24 * 60 * 60 * 1000);
            hour = (diff / (60 * 60 * 1000) - day * 24);
            min = ((diff / (60 * 1000)) - day * 24 * 60 - hour * 60);
            sec = (diff/1000-day*24*60*60-hour*60*60-min*60);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return day + "天" + hour + "小时" + min + "分" + sec + "秒";
    }
}
